home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 June / PersonalComputerWorld-June2009-CoverdiscCD.iso / Software / Shareware / Mockups for Desktop 1.5.27 / MockupsForDesktop.air / MockupsAir.swf / scripts / LocalDBManager.as < prev    next >
Encoding:
Text File  |  2009-03-03  |  4.7 KB  |  159 lines

  1. package
  2. {
  3.    import com.plus9.mockups.model.ICookieManager;
  4.    import com.plus9.mockups.model.IStorageManager;
  5.    import flash.data.SQLConnection;
  6.    import flash.data.SQLMode;
  7.    import flash.data.SQLStatement;
  8.    import flash.filesystem.File;
  9.    
  10.    public class LocalDBManager implements ICookieManager
  11.    {
  12.       protected var _dbFile:File;
  13.       
  14.       protected var _sqlConnection:SQLConnection;
  15.       
  16.       protected var _useIt:Boolean = true;
  17.       
  18.       protected var _sm:IStorageManager;
  19.       
  20.       public function LocalDBManager()
  21.       {
  22.          var _loc1_:SQLStatement = null;
  23.          super();
  24.          _dbFile = File.applicationStorageDirectory.resolvePath("MockupsCookies.db");
  25.          _sqlConnection = new SQLConnection();
  26.          if(!_dbFile.exists)
  27.          {
  28.             _sqlConnection.open(_dbFile,SQLMode.CREATE);
  29.             _loc1_ = new SQLStatement();
  30.             _loc1_.sqlConnection = _sqlConnection;
  31.             _loc1_.text = "CREATE TABLE cookies (propName TEXT PRIMARY KEY, propValue OBJECT);";
  32.             _loc1_.execute();
  33.             _sqlConnection.close();
  34.          }
  35.       }
  36.       
  37.       public function getProperty(param1:String, param2:Object) : Object
  38.       {
  39.          var res:Object = null;
  40.          var sqlStatement:SQLStatement = null;
  41.          var results:Array = null;
  42.          var temp:Object = null;
  43.          var p_propName:String = param1;
  44.          var p_default:Object = param2;
  45.          if(_useIt == false)
  46.          {
  47.             return p_default;
  48.          }
  49.          _sqlConnection = new SQLConnection();
  50.          try
  51.          {
  52.             _sqlConnection.open(_dbFile,SQLMode.READ);
  53.             sqlStatement = new SQLStatement();
  54.             sqlStatement.sqlConnection = _sqlConnection;
  55.             sqlStatement.text = "SELECT propValue FROM cookies WHERE propName=\'" + p_propName + "\';";
  56.             sqlStatement.execute();
  57.             results = sqlStatement.getResult().data;
  58.             if(results == null)
  59.             {
  60.                res = p_default;
  61.             }
  62.             else
  63.             {
  64.                temp = results[0];
  65.                res = temp.propValue;
  66.             }
  67.          }
  68.          catch(e:Error)
  69.          {
  70.             log("SELECT failed!:" + e.toString());
  71.             res = p_default;
  72.          }
  73.          _sqlConnection.close();
  74.          if(res == "true")
  75.          {
  76.             res = true;
  77.          }
  78.          else if(res == "false")
  79.          {
  80.             res = false;
  81.          }
  82.          log("#LDBMGR# getProperty " + p_propName + ", " + p_default + ", returning " + res);
  83.          return res;
  84.       }
  85.       
  86.       public function set storageManager(param1:IStorageManager) : void
  87.       {
  88.          _sm = param1;
  89.       }
  90.       
  91.       public function get useCookies() : Boolean
  92.       {
  93.          return _useIt;
  94.       }
  95.       
  96.       protected function dumpDB() : void
  97.       {
  98.       }
  99.       
  100.       public function set useCookies(param1:Boolean) : void
  101.       {
  102.          _useIt = param1;
  103.       }
  104.       
  105.       protected function log(param1:String) : void
  106.       {
  107.          if(_sm)
  108.          {
  109.             _sm.log(param1);
  110.          }
  111.       }
  112.       
  113.       public function setProperty(param1:String, param2:Object) : void
  114.       {
  115.          var valueToAdd:Object;
  116.          var sqlStatement:SQLStatement = null;
  117.          var p_propName:String = param1;
  118.          var p_value:Object = param2;
  119.          if(_useIt == false)
  120.          {
  121.             return;
  122.          }
  123.          log("#LDBMGR# setProperty: " + p_propName + ", " + p_value);
  124.          valueToAdd = p_value;
  125.          if(p_value is Boolean)
  126.          {
  127.             valueToAdd = p_value + "";
  128.          }
  129.          else
  130.          {
  131.             valueToAdd = p_value;
  132.          }
  133.          _sqlConnection = new SQLConnection();
  134.          try
  135.          {
  136.             _sqlConnection.open(_dbFile,SQLMode.UPDATE);
  137.             sqlStatement = new SQLStatement();
  138.             sqlStatement.sqlConnection = _sqlConnection;
  139.             sqlStatement.text = "UPDATE cookies SET propValue=? WHERE propName=\'" + p_propName + "\';";
  140.             sqlStatement.parameters[0] = valueToAdd;
  141.             sqlStatement.execute();
  142.             if(sqlStatement.getResult().rowsAffected == 0)
  143.             {
  144.                log("#LDBMGR# inserting instead!");
  145.                sqlStatement.text = "INSERT INTO cookies (propName, propValue) VALUES (\'" + p_propName + "\',?);";
  146.                sqlStatement.parameters[0] = valueToAdd;
  147.                sqlStatement.execute();
  148.             }
  149.          }
  150.          catch(e:Error)
  151.          {
  152.             log("UPDATE failed!:" + e.toString());
  153.          }
  154.          _sqlConnection.close();
  155.       }
  156.    }
  157. }
  158.  
  159.